package com.yqg.protection.netty.handler.factory;

import com.yqg.protection.netty.handler.ProtocolHandler;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ProtocolHandlerFactory {

    private final Map<String, ProtocolHandler> handlerMap;

    // 自动注入所有ProtocolHandler实现类
    public ProtocolHandlerFactory(List<ProtocolHandler> handlers) {
        this.handlerMap = new HashMap<>();
        for (ProtocolHandler handler : handlers) {
            handlerMap.put(handler.supportHeader(), handler);
        }
    }

    // 获取对应协议的处理器
    public ProtocolHandler getHandler(String header) {
        return handlerMap.get(header);
    }
}
